//
//  AppDelegate.m
//  aigo
//
//  Created by Tai Truong on 11/20/12.
//  Copyright (c) 2012 AIGO. All rights reserved.
//

#import "AppDelegate.h"
#import "AppViewController.h"
#import "DriverMainViewController.h"
#import "FontManager.h"
#import "VKConfigDataModel.h"

@implementation AppDelegate {
    VKConfigDataModel* configDataModel;
}

- (void) mainLoop
{
// Check memory every a loop time
//    if (CFAbsoluteTimeGetCurrent() - _checkingMemoryTimer > TIMER_CHECKING_MEMORY) {
//        _checkingMemoryTimer = CFAbsoluteTimeGetCurrent();
//
//        struct task_basic_info info;
//        mach_msg_type_number_t size = sizeof(info);
//        kern_return_t kerr = task_info(mach_task_self(),
//                                       TASK_BASIC_INFO,
//                                       (task_info_t)&info,
//                                       &size);
//        if( kerr == KERN_SUCCESS ) {
//            NSLog(@"Memory in use (in megabytes): %u", info.resident_size /1048576);
//        } else {
//            NSLog(@"Error with task_info(): %s", mach_error_string(kerr));
//        }
//    }
    
    [[AppViewController Shared] update];
}

- (void)loadFontManager {
    [[FontManager sharedManager] loadFont:@"Bebas Neue"];
    [[FontManager sharedManager] loadFont:@"Century Gothic Bold"];
    [[FontManager sharedManager] loadFont:@"Century Gothic"];
    [[FontManager sharedManager] loadFont:@"Furore Regular"];
    [[FontManager sharedManager] loadFont:@"Futura Condensed Medium"];
    [[FontManager sharedManager] loadFont:@"Futura Medium"];
    [[FontManager sharedManager] loadFont:@"Great Vibes"];
    [[FontManager sharedManager] loadFont:@"Helvetica Neue Light"];
    [[FontManager sharedManager] loadFont:@"Helvetica Neue UltraLight"];
    [[FontManager sharedManager] loadFont:@"Helvetica Neue"];
    [[FontManager sharedManager] loadFont:@"HelveticaNeue Bold"];
    [[FontManager sharedManager] loadFont:@"MyriadPro-Regular"];
    [[FontManager sharedManager] loadFont:@"Rockwell"];
    [[FontManager sharedManager] loadFont:@"YournameD7FinancialHalf"];
    [[FontManager sharedManager] loadFont:@"Comfortaa-Thin"];
    [[FontManager sharedManager] loadFont:@"Comfortaa-Bold"];
    [[FontManager sharedManager] loadFont:@"Comfortaa"];
}

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    // Override point for customization after application launch.
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:[AppViewController Shared]];
    navController.view.backgroundColor = [UIColor blackColor];
    navController.navigationBar.backgroundColor = [UIColor blackColor];
    
    [navController setNavigationBarHidden:YES];
    if ([[[UIDevice currentDevice] systemVersion] floatValue] < 4) {
        [self.window addSubview:navController.view];
    }
    else{
        self.window.rootViewController = navController;
    }
    
    configDataModel = [AppViewController Shared].configDataModel;
    
    application.applicationSupportsShakeToEdit = YES;
    [[AppViewController Shared] changeToSplashScreen];
    
    [[UIApplication sharedApplication] registerForRemoteNotificationTypes:(UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound)];
    
    _timerMain = [NSTimer scheduledTimerWithTimeInterval:TIMER_MAIN_LOOP target:self selector:@selector(mainLoop) userInfo:nil repeats:YES];
    [[NSRunLoop mainRunLoop] addTimer:_timerMain forMode:NSRunLoopCommonModes];
    
    return YES;
}

- (void)applicationWillResignActive:(UIApplication *)application
{
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
    
    NSLog(@"applicationWillResignActive");
    UIApplication *app = [UIApplication sharedApplication];
    
    // Request permission to run in the background. Provide an
    // expiration handler in case the task runs long.
    NSAssert(_bgTask == UIBackgroundTaskInvalid, nil);
    
    _bgTask = [app beginBackgroundTaskWithExpirationHandler:^{
        // Synchronize the cleanup call on the main thread in case
        // the task actually finishes at around the same time.
        dispatch_async(dispatch_get_main_queue(), ^{
            
            if (_bgTask != UIBackgroundTaskInvalid)
            {
                [app endBackgroundTask:_bgTask];
                _bgTask = UIBackgroundTaskInvalid;
            }
        });
    }];
    
    // Start the long-running task and return immediately.
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        
        // Do the work associated with the task.
        
        [[AutomationDataManager Shared] stopUpdateLocationService];
        [[AutomationDataManager Shared] startMonitoringSignificantLocationChanges];
        
        //        NSLog(@"App staus: applicationDidEnterBackground");
        // Synchronize the cleanup call on the main thread in case
        // the expiration handler is fired at the same time.
        dispatch_async(dispatch_get_main_queue(), ^{
            if (_bgTask != UIBackgroundTaskInvalid)
            {
                [app endBackgroundTask:_bgTask];
                _bgTask = UIBackgroundTaskInvalid;
            }
        });
    });
    
    //    NSLog(@"backgroundTimeRemaining: %.0f", [[UIApplication sharedApplication] backgroundTimeRemaining]);
}



- (void)applicationDidBecomeActive:(UIApplication *)application
{
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
    NSLog(@"applicationDidBecomeActive");
    [[AutomationDataManager Shared] stopMonitoringSignificantLocationChanges];
    [[AutomationDataManager Shared] startUpdateLocationService];
    
}

- (void)scheduleAlarmForDate:(NSDate*)theDate
{
    UIApplication* app = [UIApplication sharedApplication];
    NSArray*    oldNotifications = [app scheduledLocalNotifications];
    
    // Clear out the old notification before scheduling a new one.
    if ([oldNotifications count] > 0)
        [app cancelAllLocalNotifications];
    
    // Create a new notification.
    UILocalNotification* alarm = [UILocalNotification new];
    if (alarm)
    {
        alarm.fireDate = theDate;
        alarm.timeZone = [NSTimeZone defaultTimeZone];
        alarm.repeatInterval = 0;
        alarm.soundName = UILocalNotificationDefaultSoundName;
        alarm.alertBody = @"Countdown Time is over.";
        
        [app scheduleLocalNotification:alarm];
    }
}

- (void)applicationDidEnterBackground:(UIApplication *)application
{
    /*
     Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
     If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
     */
    NSLog(@"applicationDidEnterBackground");
    if (_timerMain) {
        [_timerMain invalidate];
        _timerMain = [NSTimer scheduledTimerWithTimeInterval:TIMER_MAIN_LOOP_BACKGROUND target:self selector:@selector(mainLoop) userInfo:nil repeats:YES];
    }
    
    // Timer Count Down
    [AppViewController Shared].backgroundTimeInterval = CFAbsoluteTimeGetCurrent();
    NSTimeInterval remainingTime = [AppViewController Shared].remainingTime;
    if (remainingTime > 0) {
        NSDate *remainingDate = [NSDate dateWithTimeIntervalSinceNow:remainingTime];
        [self scheduleAlarmForDate:remainingDate];
        
    }
}

- (void)applicationWillEnterForeground:(UIApplication *)application
{
    /*
     Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
     */
    NSLog(@"applicationDidEnterBackground");
    if (_timerMain) {
        [_timerMain invalidate];
        _timerMain = [NSTimer scheduledTimerWithTimeInterval:TIMER_MAIN_LOOP target:self selector:@selector(mainLoop) userInfo:nil repeats:YES];
        [[NSRunLoop mainRunLoop] addTimer:_timerMain forMode:NSRunLoopCommonModes];
    }
    
    // Timer Count Down
    NSTimeInterval temp = CFAbsoluteTimeGetCurrent() - [AppViewController Shared].backgroundTimeInterval;
    NSTimeInterval remainingTime = [AppViewController Shared].remainingTime;
    if ((remainingTime - temp) > 0) {
        [AppViewController Shared].remainingTime -= temp;
    } else {
        [AppViewController Shared].remainingTime = 0;
    }
}


- (void)applicationWillTerminate:(UIApplication *)application
{
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}

- (void)application:(UIApplication *)app didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
    
    NSString * oldToken = [configDataModel deviceToken];
    
    NSString *newToken = [deviceToken description];
    newToken = [newToken stringByTrimmingCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"<>"]];
    newToken = [newToken stringByReplacingOccurrencesOfString:@" " withString:@""];
    
    NSLog(@"didRegisterForRemoteNotificationsWithDeviceToken: %@", newToken);
    
    [configDataModel setDeviceToken:newToken];
    
    if(![oldToken isEqualToString:newToken])
    {
        // request update token
        [[AppViewController Shared] requestUpdateDeviceToken:oldToken];
    }

}

- (void)application:(UIApplication *)app didFailToRegisterForRemoteNotificationsWithError:(NSError *)err {
    
    NSLog(@"didFailToRegisterForRemoteNotificationsWithError: error = %@", err);
}

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
    
    NSLog(@"didReceiveRemoteNotification: %@", userInfo);
    enumRemoveNotificationType notificationType = [[userInfo valueForKey:@"type"] integerValue];
    
    [[AppViewController Shared] didReceiveRemoteNotification:userInfo andNotificationType:notificationType];
    
}

@end
